文件上传在现在的功能需求中是很常见的,基本上所有的项目都会涉及到文件的上传,接下来就介绍文件处理当中的文件上传功能,仅仅是提供一个例子,希望对你们有所帮助
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public Object File(@RequestParam("fileName") MultipartFile file, HttpServletRequest request, HttpServletResponse response) { Files files = new Files(); String fileName = file.getOriginalFilename(); String NewFileName = request.getParameter("NewFileName"); String suffixName = fileName.substring(fileName.lastIndexOf(".")); String filePath = "F:/upload/";
String NewFileNames = UUID.randomUUID() + "-" + fileName; File dest = new File(filePath + NewFileNames); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdir(); } try { file.transferTo(dest); response.sendRedirect("http://127.0.0.1:8080/html/file/files.html"); } catch (IOException e) { e.printStackTrace(); } return "上传失败,请返回页面重新上传!"; }
|
注:如有需要,可自行转载,但是要加上原创作者及原文章链接哦…